package com.lijie.basecommon.exception;

import com.lijie.utilscommon.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 哈喽;
 * @create 2020-07-20 20:53
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error();
    }

    @ResponseBody
    @ExceptionHandler(value = ArithmeticException.class)
    public Result handleArithmeticException(ArithmeticException  e) {
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().message("算术异常");
    }

    @ResponseBody
    @ExceptionHandler(value = NullPointerException.class)
    public Result handleArithmeticException(NullPointerException  e) {
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().message("空指针异常");
    }
    @ResponseBody
    @ExceptionHandler(value = ArrayIndexOutOfBoundsException.class)
    public Result handleArithmeticException(ArrayIndexOutOfBoundsException  e) {
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().message("越界异常");
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result error(BusinessException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().message(e.getMsg()).code(e.getCode());
    }


}